package com.itboye.jigongbao.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.itboye.jigongbao.R
import com.itboye.jigongbao.base.BaseActivity
import com.itboye.jigongbao.bean.JiYiBiBean
import com.itboye.jigongbao.enums.DetailEnums
import com.itboye.jigongbao.fragments.jiyibi_fragment.JiGongFragment
import com.itboye.jigongbao.utils.Const
import com.itboye.jigongbao.utils.SPUtils
import com.itboye.jigongbao.utils.SPUtils.getSp
import com.itboye.jigongbao.utils.TitleBarUtils
import kotlinx.android.synthetic.main.activity_ji_yi_bi.*

class JiYiBiActivity : BaseActivity() {

    var txt_jiyibi_0: TextView? = null
    var txt_jiyibi_1: TextView? = null
    var txt_jiyibi_2: TextView? = null
    var txt_jiyibi_3: TextView? = null
    var iv_actionbar_left: ImageView? = null
    var index: Int? = 0
    var tempIndex: Int? = -1
    var baoGongFragent: Fragment? = null
    var baoGongFragent2: Fragment? = null
    var baoGongFragent3: Fragment? = null
    var baoGongFragent4: Fragment? = null
    var fragmentManager: FragmentManager? = null
    var transaction: FragmentTransaction? = null
    var type: String? = null
    var role: String? = null
    var item_type_key: String? = null
    var model: JiYiBiBean.JiYiBiEntity? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_ji_yi_bi
    }

    var bundle: Bundle? = null

    override fun initData() {
        TitleBarUtils().initTitlebarStyle1(this, this!!.trans_action_bar_jiyibi!!, "记一笔", R.drawable.img_back, "", 0, "")
        type = intent.getStringExtra("type")
        role = SPUtils.getSp(Const.ROLE).toString()
        item_type_key = intent.getStringExtra("item_type_key")
        when (item_type_key) {
            "00S002002" -> {
                index = 1
            }
            "00S002003" -> {
                index = 0
            }
            "00S002004" -> {
                index = 2
            }
            "00S002005" -> {
                index = 3
            }
            else -> {
                index = 0
            }
        }
        initButtonStatus()
    }


    private fun initButtonStatus() {
        txt_jiyibi_0?.isSelected = true
        txt_jiyibi_1?.isSelected = false
        txt_jiyibi_2?.isSelected = false
        txt_jiyibi_3?.isSelected = false
        if (getSp(Const.ROLE).equals(Const.WORKER)) {
            txt_jiyibi_0!!.text = "点工"
            txt_jiyibi_1!!.text = "包工"
        } else {
            txt_jiyibi_0!!.text = "记点工"
            txt_jiyibi_1!!.text = "记包工"
        }
        setSelectTab(index!!, false)
    }

    override fun onClick(p0: View?) {
        when (p0) {
            txt_jiyibi_0 -> {
                index = 0
                var selectStatus = txt_jiyibi_0!!.isSelected
                setSelectTab(index!!, selectStatus)
            }
            txt_jiyibi_1 -> {
                index = 1
                var selectStatus = txt_jiyibi_1!!.isSelected
                setSelectTab(index!!, selectStatus)
            }
            txt_jiyibi_2 -> {
                index = 2
                var selectStatus = txt_jiyibi_2!!.isSelected
                setSelectTab(index!!, selectStatus)
            }
            txt_jiyibi_3 -> {
                index = 3
                var selectStatus = txt_jiyibi_3!!.isSelected
                setSelectTab(index!!, selectStatus)
            }
            iv_actionbar_left -> {
                finish()
            }
            else -> {
            }
        }
    }

    var arrayJiYiBi: ArrayList<JiYiBiBean.JiYiBiEntity>? = null
    fun setSelectTab(index: Int, selectStatus: Boolean) {
        if (tempIndex == index) {
            return
        }
        fragmentManager = supportFragmentManager
        transaction = fragmentManager!!.beginTransaction()
        bundle = Bundle()
        bundle!!.putString("type", type)
        if (type.equals("update")) {
            bundle!!.putString("id", intent.getStringExtra("id"))
//            model = intent.getSerializableExtra("model") as JiYiBiBean.JiYiBiEntity
            bundle!!.putString("item_type_key", item_type_key)
            if (intent.getSerializableExtra("array") != null) {
                arrayJiYiBi = intent.getSerializableExtra("array") as ArrayList<JiYiBiBean.JiYiBiEntity>
            }

        }
        clearStatus()//先清除状态
        var hasFind = false
        when (index) {
            0 -> {
                arrayJiYiBi?.forEachIndexed { index, jiYiBiEntity ->
                    if (jiYiBiEntity.item_type_key.equals("00S002003")) {
                        bundle!!.putSerializable("model", jiYiBiEntity)
                        hasFind = true
                        return@forEachIndexed
                    } else {
                    }
                }
                if (hasFind) {
                    bundle!!.putString("type", "update")
                } else {
                    if (intent.getSerializableExtra("model") != null && item_type_key.equals("00S002003")) {
                        bundle!!.putString("type", "update")
                        bundle!!.putSerializable("model", intent.getSerializableExtra("model"))
                    } else {
                        bundle!!.putString("type", "add")
                    }
                }
                if (selectStatus) {
                    txt_jiyibi_0!!.setTextColor(resources.getColor(R.color.main_blue))
                    txt_jiyibi_0!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))
                } else {


                    if (baoGongFragent == null) {
                        baoGongFragent = JiGongFragment((if (role.equals(Const.FORCEMAN)) {
                            DetailEnums.YIJIBI_FOREMAN_DIANGONG
                        } else {
                            DetailEnums.YIJIBI_WORKER_DIANGONG
                        }))


                        baoGongFragent!!.arguments = bundle
                        transaction!!.add(R.id.frame_jiyibi, baoGongFragent)
                    } else {
                        // 如果HomeFragment不为空，则直接将它显示出来
                        transaction!!.show(baoGongFragent)
                    }
                    txt_jiyibi_0!!.setTextColor(resources.getColor(R.color.white))
                    txt_jiyibi_0!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_green_border_none_jiyibi))
                }
                txt_jiyibi_0!!.isSelected = (!selectStatus)
            }
            1 -> {
//                if (item_type_key.equals("00S002002")) {
//                    bundle!!.putSerializable("model", model)
//                }
                arrayJiYiBi?.forEachIndexed { index, jiYiBiEntity ->
                    if (jiYiBiEntity.item_type_key.equals("00S002002")) {
                        bundle!!.putSerializable("model", jiYiBiEntity)
                        bundle!!.putString("type", "update")
                        hasFind = true
                        return@forEachIndexed
                    }
                }
                if (hasFind) {
                    bundle!!.putString("type", "update")
                } else {
                    if (intent.getSerializableExtra("model") != null && item_type_key.equals("00S002002")) {
                        bundle!!.putString("type", "update")
                        bundle!!.putSerializable("model", intent.getSerializableExtra("model"))
                    } else {
                        bundle!!.putString("type", "add")
                    }
                }
                if (selectStatus) {

                    txt_jiyibi_1!!.setTextColor(resources.getColor(R.color.main_blue))
                    txt_jiyibi_1!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))

                } else {
                    if (baoGongFragent2 == null) {
                        baoGongFragent2 = JiGongFragment((if (role.equals(Const.FORCEMAN)) {
                            DetailEnums.YIJIBI_FOREMAN_BAOGONG
                        } else {
                            DetailEnums.YIJIBI_WORKER_BAOGONG
                        }))

                        baoGongFragent2!!.arguments = bundle
                        transaction!!.add(R.id.frame_jiyibi, baoGongFragent2)
                    } else {
                        // 如果HomeFragment不为空，则直接将它显示出来
                        transaction!!.show(baoGongFragent2)
                    }
                    txt_jiyibi_1!!.setTextColor(resources.getColor(R.color.white))
                    txt_jiyibi_1!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_green_border_none_jiyibi))
                }
                txt_jiyibi_1!!.isSelected = (!selectStatus)
            }
            2 -> {
                arrayJiYiBi?.forEachIndexed { index, jiYiBiEntity ->
                    if (jiYiBiEntity.item_type_key.equals("00S002004")) {
                        bundle!!.putSerializable("model", jiYiBiEntity)
                        bundle!!.putString("type", "update")
                        hasFind = true
                        return@forEachIndexed
                    }
                }
                if (hasFind) {
                    bundle!!.putString("type", "update")
                } else {
                    if (intent.getSerializableExtra("model") != null && item_type_key.equals("00S002004")) {
                        bundle!!.putString("type", "update")
                        bundle!!.putSerializable("model", intent.getSerializableExtra("model"))
                    } else {
                        bundle!!.putString("type", "add")
                    }
                }
                if (selectStatus) {
                    txt_jiyibi_2!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))
                    txt_jiyibi_2!!.setTextColor(resources.getColor(R.color.main_blue))

                } else {

                    if (baoGongFragent3 == null) {
                        baoGongFragent3 = JiGongFragment((if (role.equals(Const.FORCEMAN)) {
                            DetailEnums.YIJIBI_FOREMAN_JIEZHI
                        } else {
                            DetailEnums.YIJIBI_WORKER_JIEZHI
                        }))

                        baoGongFragent3!!.arguments = bundle
                        transaction!!.add(R.id.frame_jiyibi, baoGongFragent3)
                    } else {
                        // 如果HomeFragment不为空，则直接将它显示出来
                        transaction!!.show(baoGongFragent3)
                    }
                    txt_jiyibi_2!!.setTextColor(resources.getColor(R.color.white))
                    txt_jiyibi_2!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_green_border_none_jiyibi))
                }

                txt_jiyibi_2!!.isSelected = (!selectStatus)

            }
            3 -> {
                arrayJiYiBi?.forEachIndexed { index, jiYiBiEntity ->
                    if (jiYiBiEntity.item_type_key.equals("00S002005")) {
                        bundle!!.putSerializable("model", jiYiBiEntity)
                        bundle!!.putString("type", "update")
                        hasFind = true
                        return@forEachIndexed
                    }
                }
                if (hasFind) {
                    bundle!!.putString("type", "update")
                } else {
                    if (intent.getSerializableExtra("model") != null && item_type_key.equals("00S002005")) {
                        bundle!!.putString("type", "update")
                        bundle!!.putSerializable("model", intent.getSerializableExtra("model"))
                    } else {
                        bundle!!.putString("type", "add")
                    }
                }
//                if (item_type_key.equals("00S002005")) {
//                    bundle!!.putSerializable("model", model)
//                }
                if (selectStatus) {

                    txt_jiyibi_3!!.setTextColor(resources.getColor(R.color.main_blue))
                    txt_jiyibi_3!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))
                } else {
                    if (baoGongFragent4 == null) {
                        baoGongFragent4 = JiGongFragment((if (role.equals(Const.FORCEMAN)) {
                            DetailEnums.YIJIBI_FOREMAN_JIESUAN
                        } else {
                            DetailEnums.YIJIBI_WORKER_JIESUAN
                        }))

                        baoGongFragent4!!.arguments = bundle
                        transaction!!.add(R.id.frame_jiyibi, baoGongFragent4)
                    } else {
                        // 如果HomeFragment不为空，则直接将它显示出来
                        transaction!!.show(baoGongFragent4)
                    }
                    txt_jiyibi_3!!.setTextColor(resources.getColor(R.color.white))
                    txt_jiyibi_3!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_green_border_none_jiyibi))
                }
                txt_jiyibi_3!!.isSelected = (!selectStatus)
            }
            else -> {
            }
        }
        tempIndex = index
        transaction!!.commit()
    }

    private fun clearStatus() {
        txt_jiyibi_0!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))
        txt_jiyibi_1!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))
        txt_jiyibi_2!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))
        txt_jiyibi_3!!.setBackgroundDrawable(resources.getDrawable(R.drawable.btn_oval_bg_white_border_blue_jiyibi))

        txt_jiyibi_0!!.isSelected = false
        txt_jiyibi_1!!.isSelected = false
        txt_jiyibi_2!!.isSelected = false
        txt_jiyibi_3!!.isSelected = false

        txt_jiyibi_0!!.setTextColor(resources.getColor(R.color.main_blue))
        txt_jiyibi_1!!.setTextColor(resources.getColor(R.color.main_blue))
        txt_jiyibi_2!!.setTextColor(resources.getColor(R.color.main_blue))
        txt_jiyibi_3!!.setTextColor(resources.getColor(R.color.main_blue))

        if (baoGongFragent != null) {
            if (transaction != null) {
                transaction!!.hide(baoGongFragent)
            }
        }
        if (baoGongFragent2 != null) {
            if (transaction != null) {
                transaction!!.hide(baoGongFragent2)
            }
        }
        if (baoGongFragent3 != null) {
            if (transaction != null) {
                transaction!!.hide(baoGongFragent3)
            }
        }
        if (baoGongFragent4 != null) {
            if (transaction != null) {
                transaction!!.hide(baoGongFragent4)
            }
        }
    }
}
